<?php
declare (strict_types = 1);

namespace app\controller\admin;

use app\controller\admin\AdminBaseController;
use think\Request;
use app\model\admin\SysDept as SysDeptModel;


class SysDept extends AdminBaseController
{

    public function index(Request $request)
    {
        $field = "*";
        $data = SysDeptModel::scope('where', $request->param())
            ->scope('dataFilter')
            ->field($field)
            ->order('order_num','asc')
            ->select();
        
        return success($data);
    }

    

    public function show(Request $request)
    {
        $id = $request->param('id');
        $data = SysDeptModel::where('id', $id)->scope('dataFilter')->find();

        if(!$data){
            return error('数据不存在');
        }
        return success($data);
    }

    public function exclude(Request $request){
        $id = $request->param('id');
        $field = "*";
        $data = SysDeptModel::where('id', '<>', $id)->where('ancestors', 'not like', '%'.$id.'%')
        // ->whereOr([['id', '<>', $id],['id', 'not like', '%'.$id.'%'],])
        ->scope('dataFilter')
            ->field($field)
            ->order('order_num','asc')
            ->select();
        return success($data);
    }
    public function save(Request $request)
    {
        $param = $request->param();
        $parent_id = $param['parent_id'] ?? 0;
        $dept_name = $param['dept_name'] ?? '';
        $order_num = $param['order_num'] ?? 0;
        $leader = $param['leader']  ?? '';
        $phone = $param['phone']  ?? '';
        $email = $param['email']  ?? '';
        $status = $param['status']  ?? '0';

        $ancestors = $this->getDeptAncestors($parent_id);

        $data = SysDeptModel::create([
            'parent_id' => $parent_id,
            'dept_name' => $dept_name,
            'order_num' => $order_num,
            'leader' => $leader,
            'phone' => $phone,
            'email' => $email,
            'status' => $status,
            'ancestors' => $ancestors
        ]);

        if($data->id){
            return success($data);
        }
        return error('添加失败');
    }

    public function update(Request $request)
    {
        $id = $request->param('id');
        if(!$id){
            return error('缺少必要参数');
        }
        
        $model = SysDeptModel::where('id', $id)->scope('dataFilter')->find();

        $data = $request->only(['parent_id','dept_name','order_num','leader','phone','email','status']);

        $parent_id = $data['parent_id'];
        if($parent_id != $model->parent_id){
            $data['ancestors'] = $this->getDeptAncestors($parent_id);
        }
        
        $ret = $model->save($data);
        if(!$ret){
            return error('更新失败');
        }
        return success();
    }
    public function delete(Request $request)
    {
        $id = $request->param('id', '');
        if(!$id){
            return error('缺少必要参数');
        }
        $ids = explode(',', $id);
        $ret = SysDeptModel::destroy($ids);
        if(!$ret){
            return error('删除失败');
        }
        return success();
    }

    protected function getDeptAncestors($parent_id)
    {
        $has_parent = true;

        $parent = [];
        while($has_parent == true){
            $dept = SysDeptModel::find($parent_id);
            if($dept && $dept->id > 0){
                array_unshift($parent, $dept->id);
                // $ancestors = $ancestors.','.$dept->id;
                $parent_id = $dept->parent_id;
                
            }
            if($parent_id <= 0){
                $has_parent = false;
                break;
            }
        }
        array_unshift($parent, 0);
        return implode(',', $parent);
    }

    // 根据角色ID查询部门树结构
    public function tree(Request $request){
        $data = SysDeptModel::where('status', '1')->field('id,dept_name,parent_id,order_num')->select();
        $menus = makeTree($data->toArray(),'id', 'parent_id');
        return success($menus);
    }


}
